package com.kangyi.user.controller;

import com.kangyi.dto.NursingHomeDetailDTO;
import com.kangyi.dto.NursingHomeFilterDTO;
import com.kangyi.dto.NursingHomePageQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.user.service.NursingHomeServices;
import com.kangyi.vo.NursingHomeHotVO;
import com.kangyi.vo.NursingHomeRecommendVO;
import com.kangyi.vo.NursingHomeDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 于建宝
 * @version 1.0
 */
@RestController
@RequestMapping("/user/nursing")
@Slf4j
@Api(tags = "养老院相关接口 ")
public class NursingHomeController {

    @Autowired
    private NursingHomeServices nursingHomeService;


    @GetMapping("/inquiry")
    @ApiOperation("查询养老院详情")
    public Result<NursingHomeDetailVO> inquiryNursingHomeById(NursingHomeDetailDTO nursingHomeDetailDTO) {
        log.info("查询养老院详情: {}", nursingHomeDetailDTO);
        try {
            NursingHomeDetailVO nursingHomeDetailVO = nursingHomeService.inquiryNursingHomeById(nursingHomeDetailDTO);
            return Result.success(nursingHomeDetailVO, "查询成功!");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询热门养老院
     * @param
     * @return
     */
    @GetMapping("/hot")// 使用Spring MVC 中的注解，表明该方法是一个处理 HTTP GET 请求的接口，请求路径为 /hot。当客户端向该路径发送 GET 请求时，会调用此方法
    @ApiOperation("查询热门养老院")//这是 Swagger 框架中的注解，用于为该接口添加描述信息，方便开发人员在 Swagger UI 等工具中查看接口的功能和用途，这里表明该接口的作用是查询热门养老院。
    public Result<List<NursingHomeHotVO>> HotNursingHome(@RequestParam BigDecimal rating){//定义一个方法，返回值为Result自定义工具类,自定义一个方法名用于调用，定义传回参数，@RequestParam 是 Spring MVC 中的注解，用于从请求的参数中获取 rating 参数的值，并将其转换为 BigDecimal 类型
        //使用try catch 将代码块包裹，如果发生异常调用 Result 类的 error 方法，将异常信息封装成统一的错误响应对象并返回给客户端。
        try {
            //使用 lombok,在控制台打印运行信息
            log.info("查询热门养老院{}",rating);
            //紧接着调用 nursingHomeService 服务层的 HotNursingHome 方法，传入 rating 参数，获取符合条件的热门养老院数据，并将结果存储在 nursingHomeHotVOList 列表中。
            List<NursingHomeHotVO> nursingHomeHotVOList = nursingHomeService.HotNursingHome(rating);
            return Result.success(nursingHomeHotVOList,"查询成功！");//如果查询成功，调用 Result 类的 success 方法，将查询结果和成功消息封装成统一的响应对象并返回给客户端。
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



    @GetMapping("/recommend")
    @ApiOperation("推荐养老院")
    public Result<List<NursingHomeRecommendVO>> NursingHomeRecommend(NursingHomeFilterDTO nursingHomeFilterDTO){
        log.info("推荐养老院：{}",nursingHomeFilterDTO);
        try {
            List<NursingHomeRecommendVO> nursingHomeRecommendVOList = nursingHomeService.NursingHomeRecommend(nursingHomeFilterDTO);
            return Result.success(nursingHomeRecommendVOList,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/page")
    @ApiOperation("养老院分页接口")
    public Result<PageResult> pageNursingHome(NursingHomePageQueryDTO nursingHomePageQueryDTO){
        try {
            log.info("分页查询：{}",nursingHomePageQueryDTO);
            PageResult pageResult = nursingHomeService.pageQuery(nursingHomePageQueryDTO);
            return Result.success(pageResult, "查询成功!");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
